﻿namespace Dcms.UpdateModule
{
    using Dcms.Common;
    using System;
    using System.Collections;
    using System.Data;
    using System.Text.RegularExpressions;
    using System.Web;

    public class DcmsCateProcessCode
    {
        public DataTable CateList(string SqlStr)
        {
            ArrayList paraNameArr = new ArrayList();
            ArrayList constDefaultvalueArr = new ArrayList();
            string[] strArray = SqlStr.Split(new char[] { '$' });
            string tempSqlStr = strArray[0];
            string pageSize = strArray[1];
            string selectDir = strArray[3];
            string[] strArray2 = strArray[4].Split(new char[] { ',' });
            paraNameArr = DcmsCommSqlProcess.GetParaNameArr(strArray[0], ref constDefaultvalueArr);
            if (paraNameArr.Count > 0)
            {
                for (int i = 0; i < paraNameArr.Count; i++)
                {
                    string str5 = paraNameArr[i].ToString().ToLower();
                    if (str5 != null)
                    {
                        if (!(str5 == "cateid"))
                        {
                            if (str5 == "catekey") goto Label_0258;
                        }
                        else
                        {
                            if (strArray2[i].ToLower() == "con") tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), "(" + DcmsCommSqlProcess.GetCateListInfo(constDefaultvalueArr[i].ToString(), selectDir) + ")");
                            if (DcmsRequest.GetQueryInt("CateId", 0) != 0)
                                tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), "(" + DcmsCommSqlProcess.GetCateListInfo(HttpContext.Current.Request.QueryString["CateId"].ToString(), selectDir) + ")");
                            else
                                tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), "(" + DcmsCommSqlProcess.GetCateListInfo(constDefaultvalueArr[i].ToString(), selectDir) + ")");
                        }
                    }
                    goto Label_0487;
                Label_0258:
                    if (strArray2[i].ToLower() == "con")
                        tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), "(" + DcmsCommSqlProcess.GetCateListInfo(constDefaultvalueArr[i].ToString(), selectDir) + ")");
                    else if (DcmsRequest.GetQueryInt("CateKey", 0) != 0)
                        tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), "(" + DcmsCommSqlProcess.GetCateListInfo(constDefaultvalueArr[i].ToString(), selectDir) + ")");
                    else if (DcmsRequest.ValidaQueryStr("CateKey"))
                        tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), "(" + DcmsCommSqlProcess.GetCateListInfo(Utils.check_TxtSql(HttpContext.Current.Request.QueryString["CateKey"].ToString()), selectDir) + ")");
                    else
                        tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), "(" + DcmsCommSqlProcess.GetCateListInfo(constDefaultvalueArr[i].ToString(), selectDir) + ")");
                Label_0487:;
                }
            }
            return DcmsCommSqlProcess.ReturnTableData(tempSqlStr, pageSize, null);
        }

        public string CateModuleSql(string ModelName, string FileList, string ParaName, string DefaultValue, string SelectDir, string ConditionStr, string NumStr, string IsPage, string OrderFileStr, string SortStr, string ParaTypeStr)
        {
            string str = "";
            string str2 = "";
            if (NumStr == "") NumStr = "1";
            if (OrderFileStr == "") OrderFileStr = "Cate_ID";
            if (SortStr == "") SortStr = "desc";
            if (IsPage == "") IsPage = "false";
            if (ParaTypeStr == "") ParaTypeStr = "con";
            string[] strArray = ParaTypeStr.Split(new char[] { ',' });
            string str3 = ParaTypeStr;
            if (ParaName != "")
            {
                string[] strArray2 = ParaName.Split(new char[] { ',' });
                string[] strArray3 = DefaultValue.Split(new char[] { '|' });
                string str4 = "";
                for (int i = 0; i < strArray2.Length; i++)
                {
                    string str6 = strArray2[i].ToLower();
                    if (str6 != null)
                    {
                        if (!(str6 == "cateid"))
                        {
                            if (str6 == "catekey") goto Label_01AA;
                        }
                        else
                        {
                            if (strArray3.Length > i)
                                str4 = strArray3[i];
                            else
                                str4 = "1";
                            if (strArray.Length < i) str3 = str3 + strArray[strArray.Length - 1];
                            str = str + "Cate_ID in [CateId#" + str4 + "] and ";
                        }
                    }
                    goto Label_0206;
                Label_01AA:
                    if (strArray3.Length > i)
                        str4 = strArray3[i];
                    else
                        str4 = "1";
                    if (strArray.Length < i) str3 = str3 + strArray[strArray.Length - 1];
                    str = str + "Cate_ID in [CateKey#" + str4 + "] and ";
                Label_0206:;
                }
            }
            ConditionStr = ConditionStr.ToLower();
            if (str.Length > 4)
                str = str + "Cate_State='1'";
            else
                str = "Cate_State='1'";
            if (IsPage.ToLower() == "true")
            {
                str2 = "select " + FileList + " from Dcms_Cate where " + str + " order by " + OrderFileStr + " " + SortStr;
                return (str2 + "$" + NumStr + "$" + ModelName + "$" + SelectDir + "$" + str3);
            }
            str2 = "select top " + NumStr + " " + FileList + " from Dcms_Cate where " + str + " order by " + OrderFileStr + " " + SortStr;
            return (str2 + "$0$" + ModelName + "$" + SelectDir + "$" + str3);
        }

        public static string getColumnListName(string sName, string sCode)
        {
            if (sCode != "")
            {
                string[] strArray = sCode.Split(new char[] { ',' });
                string str = "";
                for (int i = 0; i < strArray.Length; i++)
                {
                    if (i != strArray.Length - 1)
                        str = str + "｜&nbsp;&nbsp;";
                    else
                        str = str + "├";
                }
                return (str + sName);
            }
            return sName;
        }

        public string SpecialFieldResolve(string OldValue, string ModelName, DataRow Datadr)
        {
            string str = OldValue;
            Regex regex = new Regex("{(?<SpeFile>(" + ModelName + @"\.\w+?\.\w+?))}");
            string replacement = "";
            MatchCollection matchs = regex.Matches(OldValue);
            for (int i = 0; i < matchs.Count; i++)
            {
                string str7 = matchs[i].Groups["SpeFile"].Value.ToLower();
                if (str7 != null && str7 == "cate.idpath.path")
                {
                    string sCode = Datadr["Cate_IDPath"].ToString();
                    replacement = getColumnListName(Datadr["Cate_Name"].ToString(), sCode);
                    OldValue = Regex.Replace(OldValue, "{cate.idpath.path}", replacement, RegexOptions.IgnoreCase);
                    str = OldValue;
                }
            }
            return str;
        }
    }
}

